﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AppCommerciaux.PDF
{
    /// <summary>
    /// Logique d'interaction pour ReportViewerWPFv2.xaml
    /// </summary>
    public partial class ReportViewerWPFv2 : Window
    {
        //Passer ses identifiant en parametre d'une form à l'autre
        private Int64 identifiant_commandeClient;
        private Int64 identifiant_ligneCommandeClient;
        private long identifiant_produit;
        private Int64 identifiant_contact;
        private Int64 identiant_societe;
        private Int64 identifiant_tva; 

        public ReportViewerWPFv2()
        {
            InitializeComponent();
            _reportViewer.Load += ReportViewerWPFv2_Load;
        }

        public ReportViewerWPFv2(Int64 idCommandeClient,Int64 idLigneCommandeClient, Int64 idProduit, Int64 idContact, Int64 idSociete, Int64 idTva)
        {
            InitializeComponent();
            _reportViewer.Load += ReportViewerWPFv2_Load;

            identifiant_commandeClient = idCommandeClient;
            identifiant_ligneCommandeClient = idLigneCommandeClient;
            identifiant_produit =  idProduit;
            identifiant_contact =  idContact;
            identiant_societe =  idSociete;
            identifiant_tva =  idTva; 

        }


        private bool _isReportViewerLoaded;

        private void ReportViewerWPFv2_Load(object sender, EventArgs e)
        {
          

            try
            {
                if (!_isReportViewerLoaded)
                {
                    // Tous mes Report Data Source
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceCommandeClient = new Microsoft.Reporting.WinForms.ReportDataSource();
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceLigneCommandeClient = new Microsoft.Reporting.WinForms.ReportDataSource();
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceProduit = new Microsoft.Reporting.WinForms.ReportDataSource();
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceContact = new Microsoft.Reporting.WinForms.ReportDataSource();
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceSociete = new Microsoft.Reporting.WinForms.ReportDataSource();
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceTVA = new Microsoft.Reporting.WinForms.ReportDataSource();

                    // Initialisation de mon DataSet                 
                    liv3DataSetReportViewerCommandePDF datasetCommandeClient = new liv3DataSetReportViewerCommandePDF();
                    liv3DataSetReportViewerCommandePDF datasetLigneCommandeClient = new liv3DataSetReportViewerCommandePDF();
                    liv3DataSetReportViewerCommandePDF datasetProduit = new liv3DataSetReportViewerCommandePDF();
                    liv3DataSetReportViewerCommandePDF datasetContact = new liv3DataSetReportViewerCommandePDF();
                    liv3DataSetReportViewerCommandePDF datasetSociete = new liv3DataSetReportViewerCommandePDF();
                    liv3DataSetReportViewerCommandePDF datasetTva = new liv3DataSetReportViewerCommandePDF();
                    
                    //Chargement de quelques données

                    datasetCommandeClient.BeginInit();
                    reportDataSourceCommandeClient.Name = "DataSetCommandeClient";
                    reportDataSourceCommandeClient.Value = datasetCommandeClient.commandeclient;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceCommandeClient);
                    datasetCommandeClient.EndInit();

                    //fill data Commande Client
                    liv3DataSetReportViewerCommandePDFTableAdapters.commandeclientTableAdapter commandClientTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.commandeclientTableAdapter();
                    commandClientTableAdapter.ClearBeforeFill = true;
                    commandClientTableAdapter.FillByID(datasetCommandeClient.commandeclient, identifiant_commandeClient);

                    datasetLigneCommandeClient.BeginInit();
                    reportDataSourceLigneCommandeClient.Name = "DataSetLigneCommandeClient";
                    reportDataSourceLigneCommandeClient.Value = datasetLigneCommandeClient.lignecommandeclient;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceLigneCommandeClient);
                    datasetLigneCommandeClient.EndInit();

                    //fill data Ligne Commande Client By Id Commande
                    liv3DataSetReportViewerCommandePDFTableAdapters.lignecommandeclientTableAdapter ligneCommandClientTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.lignecommandeclientTableAdapter();
                    ligneCommandClientTableAdapter.ClearBeforeFill = true;
                    ligneCommandClientTableAdapter.FillById_CommandeClient(datasetLigneCommandeClient.lignecommandeclient,identifiant_ligneCommandeClient);
                    
                    datasetProduit.BeginInit();
                    reportDataSourceProduit.Name = "DataSetProduit";
                    reportDataSourceProduit.Value = datasetProduit.produit;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceProduit);
                    datasetProduit.EndInit();

                    //fill data Produit By Id Produit
                    liv3DataSetReportViewerCommandePDFTableAdapters.produitTableAdapter produitTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.produitTableAdapter();
                    produitTableAdapter.ClearBeforeFill = true;
                    produitTableAdapter.FillById_Produit(datasetProduit.produit, identifiant_produit);
                    

                    datasetContact.BeginInit();
                    reportDataSourceContact.Name = "DataSetContact";
                    reportDataSourceContact.Value = datasetContact.tiers_contact;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceContact);
                    datasetContact.EndInit();

                    //fill data Contact
                    liv3DataSetReportViewerCommandePDFTableAdapters.tiers_contactTableAdapter contactTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.tiers_contactTableAdapter();
                    contactTableAdapter.ClearBeforeFill = true;
                    contactTableAdapter.FillByID(datasetContact.tiers_contact, identifiant_contact);


                    datasetSociete.BeginInit();
                    reportDataSourceSociete.Name = "DataSetSociete";
                    reportDataSourceSociete.Value = datasetSociete.tiers_societe;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceSociete);
                    datasetSociete.EndInit();

                    //fill data Societe
                    liv3DataSetReportViewerCommandePDFTableAdapters.tiers_societeTableAdapter societeTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.tiers_societeTableAdapter();
                    societeTableAdapter.ClearBeforeFill = true;
                    societeTableAdapter.FillByID(datasetSociete.tiers_societe, identiant_societe);

                    datasetTva.BeginInit();
                    reportDataSourceTVA.Name = "DataSetTVA";
                    reportDataSourceTVA.Value = datasetTva.tva;
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSourceTVA);
                    datasetTva.EndInit();

                    //fill data Societe
                    liv3DataSetReportViewerCommandePDFTableAdapters.tvaTableAdapter tvaTableAdapter = new liv3DataSetReportViewerCommandePDFTableAdapters.tvaTableAdapter();
                    tvaTableAdapter.ClearBeforeFill = true;
                    tvaTableAdapter.FillByID(datasetTva.tva,identifiant_tva);

                    string cheminReport = @"C:\livrable3\ReportCommande.rdlc";
                    //string cheminReport = @"C:\Users\Hossein\Documents\Visual Studio 2012\Livrable3\fhag\SolutionPlastProd\AppCommerciaux\PDF\ReportCommande.rdlc";

                    this._reportViewer.LocalReport.ReportPath = cheminReport;
                    _reportViewer.RefreshReport();
                    _isReportViewerLoaded = true;

                    this._reportViewer.ReportExport += _reportViewer_ReportExport;

                 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);   
            }
        }

        void _reportViewer_ReportExport(object sender, Microsoft.Reporting.WinForms.ReportExportEventArgs e)
        {
            e.Cancel = true;
            string extension = e.Extension.Name;

            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {

                Title = "Enregistrement du fichier",
                FileName = "Commande - " + identifiant_commandeClient + "." + extension,
                CheckPathExists = true,
                // InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                InitialDirectory = @"C:\livrable3\",
                Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
                FilterIndex = 0
            };

            /*
            this._reportViewer.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);
             * */

            if (saveFileDialog.ShowDialog(this) == true)
            {
                this._reportViewer.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);
            }



        }

    }
}
